package com.smallkingson.ylib.core.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import java.lang.reflect.Field;

public abstract class YBaseDialogFragment extends DialogFragment implements View.OnClickListener {
    View loadingView;
    View failView;
    RelativeLayout rootRl;
    protected Context context;
    private OnReloadListener onReloadListener;
    private FragmentManager supportFragmentManager;

    public YBaseDialogFragment() {
    }

    public void init(FragmentActivity activity) {
        context = activity.getApplicationContext();
        supportFragmentManager = activity.getSupportFragmentManager();
        rootRl = new RelativeLayout(context);
        rootRl.setBackgroundColor(0x99ffddee);
        rootRl.setGravity(RelativeLayout.CENTER_IN_PARENT);
        loadingView = getLoadingView(activity);
        failView = getFailView(activity);
        failView.setOnClickListener(this);
        rootRl.addView(loadingView);
        rootRl.addView(failView);
        rootRl.setVisibility(View.GONE);

    }

    private void show() {

        if (rootRl.getVisibility() == View.VISIBLE) {
            return;
        }
        rootRl.setVisibility(View.VISIBLE);
        try {
            Field mViewField = Fragment.class.getDeclaredField("mView");
            mViewField.setAccessible(true);
            mViewField.set(this, null);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        show(supportFragmentManager, "");

    }

    protected abstract View getLoadingView(FragmentActivity activity);

    protected abstract View getFailView(FragmentActivity activity);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        ViewGroup parent = (ViewGroup) rootRl.getParent();

        if (parent != null) {
            parent.removeView(rootRl);
        }
        return rootRl;
    }

    @Override
    public void onClick(View v) {
        reload();
    }

    protected void reload() {

        if (onReloadListener != null) {
            onReloadListener.onReload();
        }
    }

    public void showLoading() {
        show();
        setCancelable(false);
        loadingView.setVisibility(View.VISIBLE);
        failView.setVisibility(View.GONE);
    }

    public void showFail(OnReloadListener onReloadListener) {

        show();
        setCancelable(true);
        failView.setVisibility(View.VISIBLE);
        loadingView.setVisibility(View.GONE);
        this.onReloadListener = onReloadListener;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        rootRl.setVisibility(View.GONE);
        super.onDismiss(dialog);
    }

    public static interface OnReloadListener {
        void onReload();
    }

}
